package com.gao.web.controller;

import com.gao.web.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;

@Controller
public class MessageController extends HttpServlet {
    @Autowired
    MessageService messageService;

    @RequestMapping("insertmessage")
    public String register(String username, String content, Model model) {
        String post_date = String.valueOf(System.currentTimeMillis() / 1000);
        int i = messageService.insertMessage(username, content, post_date);
        if (i == 1) {
            model.addAttribute("data", "0");
            return "/addMessage.jsp";
        } else {
            model.addAttribute("data", "1");
            return "";
        }
    }

    @RequestMapping("deletemessage")
    public String delete(String id, Model model) {
        int Id = Integer.parseInt(id);
        int i = messageService.delete(Id);
        System.out.println(i);
        if (i == 1) {
            model.addAttribute("data", "0");
            return "message.jsp";
        } else {
            model.addAttribute("data", "1");
            return "message.jsp";
        }
    }
}
